package com.queming.core.mapper;

import com.queming.core.dto.FileDto;
import com.queming.core.entity.File;
import com.queming.core.service.file.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Map;

/**
 * @author chencidiao
 * @date 2022/05/06 22:43
 **/
public abstract class FileMapperDecorator implements FileMapper{
    @Autowired
    @Qualifier("delegate")
    private FileMapper delegate;

    @Autowired
    private Map<String, StorageService> storageServices;

    @Override
    public FileDto toDto(File file) {
        FileDto fileDto = delegate.toDto(file);

        if (fileDto == null) {
            return null;
        }

       if(fileDto.getStorage()==null){
           return null;
       }

        fileDto.setUri(storageServices.get(fileDto.getStorage().name()).getFileUri(fileDto.getKey()));
        return fileDto;
    }
}
